package com.view;

import com.model.Anggota;
import com.model.Perpustakaan;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.*;

public class PanelCariAnggota extends JPanel {

    private JLabel kataKunci;
    private JTextField textCari;
    private JButton tombolCari;
    private JTextArea areaAnggota;

    public PanelCariAnggota() {
        inisialisasi();
    }

    public void inisialisasi() {
        this.setLayout(new BorderLayout());
        JPanel panelTombol = new JPanel();
        panelTombol.setLayout(new FlowLayout());

        kataKunci = new JLabel("Kata Kunci");
        kataKunci.setBounds(15, 470, 50, 20);
        panelTombol.add(kataKunci);

        textCari = new JTextField();
        textCari.setColumns(10);
        panelTombol.add(textCari);

        tombolCari = new JButton("Cari");
        panelTombol.add(tombolCari);

        areaAnggota = new JTextArea();
        areaAnggota.setBounds(100, 50, 300, 250);
        add(areaAnggota);
        setVisible(true);

        tombolCari.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                tombolCariAction();
            }
        });
        add(panelTombol, BorderLayout.NORTH);
    }

    public void tombolCariAction() {
        Perpustakaan.bacaTabelAnggota();
        ArrayList<Anggota> test4 = Perpustakaan.cariAnggota(textCari.getText());
        for (int i = 0; i < test4.size(); i++) {
            String temp = "";
            temp = "* Nama : "+ test4.get(i).getNama() +
                 "\n  Id   : " + test4.get(i).getIdAnggota();
            areaAnggota.setText(temp);
        }
    }
}
